package org.smoozik.app.ihm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Collection;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

import org.smoozik.app.mode.Mode;
import org.smoozik.app.mode.generator.GeneratorMode;
import org.smoozik.app.mode.linetrainer.LineTrainerMode;
import org.smoozik.app.mode.rythmtrainer.RythmTrainerMode;
import org.smoozik.app.settings.SettingsFrame;
import org.smoozik.services.sound.SoundManager;
import org.smoozik.services.sound.SoundPlayer;

@SuppressWarnings("serial")
public class Smoozik extends JFrame {
	private SmoozikListener smoozikListener;
	private SettingsFrame settingsFrame;
	private String paramMode;
	private HashMap<String, Mode> modes;
	private JPanel currentPanel;
	private SoundPlayer soundPlayer;

	public Smoozik() {
		super();

		soundPlayer = new SoundPlayer();

		smoozikListener = new SmoozikListener(this);
		// Menu Bar
		JMenuBar lMenuBar = new JMenuBar();
		JMenu lMenu = new JMenu(Constants.GAME_LABEL);
		lMenuBar.add(lMenu);
		JMenuItem lMenuItemSettings = new JMenuItem(Constants.SETTINGS_LABEL);
		lMenuItemSettings.addActionListener(smoozikListener);
		lMenu.add(lMenuItemSettings);
		this.setJMenuBar(lMenuBar);

		modes = new HashMap<String, Mode>();

		Mode mode = new LineTrainerMode(this);
		modes.put(mode.getId(), mode);
		mode = new GeneratorMode(this);
		modes.put(mode.getId(), mode);
		mode = new RythmTrainerMode(this);
		modes.put(mode.getId(), mode);

		this.setBackground(Color.WHITE);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		updateMode(Constants.DEFAULT_MODE);

		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				// TODO handle the change
				System.err.println("resizing");
				modes.get(paramMode).resize(getSize());
				repaint();
			}
		});
	}

	public JPanel getCurrentPanel() {
		return currentPanel;
	}

	public void setCurrentPanel(JPanel currentPanel) {
		this.currentPanel = currentPanel;
	}

	public SoundPlayer getSoundPlayer() {
		return soundPlayer;
	}

	public void setSoundPlayer(SoundPlayer soundPlayer) {
		this.soundPlayer = soundPlayer;
	}

	public Collection<Mode> getModes() {
		return modes.values();
	}

	public float getVolume() {
		float f = new SoundManager().getVolume();
		return f;
	}

	public void setVolume(float f) {
		new SoundManager().setVolume(f);
	}

	public String getParamMode() {
		return paramMode;
	}

	public void setParamMode(String paramMode) {
		this.paramMode = paramMode;
	}

	public static void main(String[] args) throws UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel(new NimbusLookAndFeel());

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Smoozik frame = new Smoozik();

				// Display the window.
				frame.setTitle(Constants.FRAME_TITLE);
				frame.pack();
				frame.setVisible(true);
				frame.setSize(1250, 1000);
			}
		});
	}

	//
	// // Settings frame
	//
	/**
	 * 
	 */
	public void showSettingsFrame() {
		if (settingsFrame == null) {
			settingsFrame = new SettingsFrame(this);
			settingsFrame.setLocationRelativeTo(this);
		}
		settingsFrame.refreshParams();
		settingsFrame.setVisible(true);
	}

	public void updateMode(String m) {
		if (m != null && !m.equals(this.paramMode)) {
			Mode mode = modes.get(m);
			JPanel mainPanel = mode.getMainPanel();
			if (currentPanel != null) {
				this.remove(currentPanel);
			}
			this.currentPanel = mainPanel;
			this.add(mainPanel, BorderLayout.CENTER);
			setParamMode(m);
			revalidate();
			repaint();
		}
	}
	
}
